package electricity.service.impl;

import electricity.domain.DeviceInfo;
import electricity.domain.RegionMetric;
import electricity.domain.SysRegion;
import electricity.mapper.DeviceInfoMapper;
import electricity.mapper.RegionMetricMapper;
import electricity.mapper.SysRegionMapper;
import electricity.service.RegionMetricService;
import electricity.vo.RegionMetricVO;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import javax.swing.plaf.synth.Region;
import java.util.List;

@Service
public class RegionMetricServiceImpl implements RegionMetricService {
    @Resource
    private SysRegionMapper sysregionMapper;

    @Resource
    private RegionMetricMapper metricMapper;

    @Resource
    private DeviceInfoMapper deviceInfoMapper;

    @Override
    public List<SysRegion> getRegionList() {
        List<SysRegion> sysRegions = sysregionMapper.selectAll();
        return sysRegions;
    }

    @Override
    public RegionMetricVO getMetricByRegion(String regionValue) {
        // 查询最新指标
        RegionMetric metric = metricMapper.selectLatestByRegion(regionValue);
        if (metric == null) {
            return new RegionMetricVO();
        }
        // 转换为 VO
        RegionMetricVO vo = new RegionMetricVO();
        BeanUtils.copyProperties(metric, vo);
        return vo;
    }

    @Override
    public List<DeviceInfo> getDeviceList(int page, int size) {
        // 计算分页起始位置（page 从 1 开始）
        int start = (page - 1) * size;
        return deviceInfoMapper.selectByPage(start, size);
    }

    @Override
    public int getDeviceTotalCount() {
        return deviceInfoMapper.selectTotalCount();
    }
}
